//////////
// Info //
//////////
function Info() {
	var i = document.getElementById("info");
	var timerId;
	
	this.showAndSetTimer = function() {
		if (this.timerId) {
			window.clearTimeout(this.timerId);
			this.timerId = null;
		}
		showElement("info");
		this.timerId = window.setTimeout(function () {
			hideElement("info");
		}, 3000);
	};
	
	this.error = function(msg) {
		i.innerHTML = msg;
		i.style.color = "white";
		i.style.backgroundColor = '#B40000';
		this.showAndSetTimer();
	};
	
	this.warn = function(msg) {
		i.innerHTML = msg;
		i.style.color = "black";
		i.style.backgroundColor = '#FAD163';
		this.showAndSetTimer();
	};
	
	this.info = function(msg) {
		i.innerHTML = msg;
		i.style.color = "white";
		i.style.backgroundColor = '#228B22';
		this.showAndSetTimer();
	};
}
/////////////////
// end of Info //
/////////////////

/////////////////////
// initLocalServer //
/////////////////////
function initLocalServer(debug) {
//	showElement("localserverinfo");
	
	var completeCall = 0;
	
	// create local server
	var localServer = google.gears.factory.create('beta.localserver');
	// create store
	var store = localServer.createManagedStore('webdb.localserver');
	
	// configure store
	// set manifest file
	store.manifestUrl = 'manifest.json';
	
	// add event handlers
	// on complete
	store.oncomplete = function (details) {
		completeCall++;
		var oncompleteElement = document.getElementById("localserver_oncomplete");
		if ("" == details.newVersion) {
			oncompleteElement.innerHTML = "nothing changed #" + completeCall;
		} else {
			oncompleteElement.innerHTML = "oncomplete #" + details.newVersion + "#";
		}
		if (debug) console.debug("store oncomplete ", details.newVersion);
		window.setTimeout(function () {
			hideElement("localserverinfo");
		}, 3000);
	};
	// on progress
	store.onprogress = function (details) {
		var onprogressElement = document.getElementById("localserver_onprogress");
		onprogressElement.innerHTML = "store onprogress " + details.filesComplete
				+ "/" + details.filesTotal;
		if (debug) console.debug("store onprogress ", details.filesComplete, "/", details.filesTotal);
	};
	//on error
	store.onerror = function (error) {
		var onerrorElement = document.getElementById("localserver_onerror");
		onerrorElement.innerHTML = "storeError(): error = " + error.message;
		if (debug) console.debug("storeError(): name = ", error.name, " message = ", error.message);
		window.setTimeout(function () {
			hideElement("localserverinfo");
		}, 3000);
	};
	
	// check for updates
	store.checkForUpdate();
}
////////////////////////////
// end of initLocalServer //
////////////////////////////

////////////
// init() //
////////////
function init() {
	window.info = new Info();
	if (!window.google || !google.gears) {
		hideAll();
		showElement("installGearsLink");
		return;
	}
	hideElement("installGearsLink");
	var permission = google.gears.factory.getPermission("WebDb by Q", "images/webdb.png");
	if (!permission) {
		info.error("You haven't allowed this web application to use Gears. "
				+ "If you want to accept this application you have to refresh this page. "
				+ "If you banned this application from access to Gears you have to remove it "
				+ "from banned applications in gears preferences and then refresh");
		hideAll();
		showElement("info");
		return;
	}
	data = new Data(true);
	refreshTableList();
	syncer = new Syncer();
	syncer.sync();
	initLocalServer(false);
}
///////////////////
// end of init() //
///////////////////